package com.quick.eventproducer;

import com.lmax.disruptor.RingBuffer;
import com.quick.event.LongEvent;

import java.nio.ByteBuffer;

/**
 * @ClassName LongEventProducer
 * @Description TODO
 * @Author wangxin43
 * @Date 2018/5/24 上午10:22
 * @Version 1.0
 **/
public class LongEventProducer {
    private final RingBuffer<LongEvent> ringBuffer;

    public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void onData(ByteBuffer bb) {
        //可以把ringBuffer看做一个事件队列，那么next就是得到下面一个事件槽
        long sequence = ringBuffer.next();
        try {
            //用上面的索引取出一个空的事件用于填充
            LongEvent event = ringBuffer.get(sequence);
            event.setValue(bb.getLong(0));
        } finally {
            //发布事件
            ringBuffer.publish(sequence);
        }
    }
}